package arch.async.event;

import arch.domain.archive.ArchiveNumber;
import arch.domain.archive.Classification;
import arch.domain.archive.Retention;
import arch.domain.user.Dept;
import arch.service.archive.ArchiveNumberService;
import arch.service.archive.ArchiveService;
import arch.util.BeanRepository;

public class ArchiveNumberSynchronizationEvent implements Runnable {
	private Integer classificationId;
	private Integer year;
	private String deptCode;
	private Integer retentionId;
	private Long beginSerial;
	private Long endSerial;
	public void run() {
		if(classificationId != null && classificationId > 0
			&& year != null && year > 0
			&& deptCode != null && !"".equals(deptCode.trim())
			&& retentionId != null && retentionId > 0
			&& beginSerial != null && beginSerial > 0
			&& endSerial != null && endSerial > 0
			&& endSerial >= beginSerial) {
			ArchiveService archiveService = BeanRepository.getBean("archiveService", ArchiveService.class);
			ArchiveNumberService archiveNumberService = BeanRepository.getBean("archiveNumberService", ArchiveNumberService.class);
			ArchiveNumber archiveNumber = new ArchiveNumber();
			Classification c = new Classification();
			archiveNumber.setClassification(c);
			Dept dept = new Dept();
			archiveNumber.setDept(dept);
			Retention r = new Retention();
			archiveNumber.setRetention(r);
			for(long i = beginSerial; i <= endSerial; i++) {
				//ArchiveNumberService.list
				archiveNumber.getClassification().setId(classificationId);
				archiveNumber.setYear(year);
				archiveNumber.getDept().setDeptCode(deptCode);
				archiveNumber.getRetention().setId(retentionId);
				archiveNumber.setSerial(i);
				String archiveId = archiveNumberService.findArchiveIdByNumber(archiveNumber);
				if(archiveId != null) {
					//ar_archive
					archiveService.setColumnValue(archiveId, "an_serial", i);
					//ar_entry
					archiveService.setEntryValue(archiveId, "ArchiveNumber", i + "");
				}
			}
		}
	}
	public Integer getClassificationId() {
		return classificationId;
	}
	public void setClassificationId(Integer classificationId) {
		this.classificationId = classificationId;
	}
	public Integer getYear() {
		return year;
	}
	public void setYear(Integer year) {
		this.year = year;
	}
	public String getDeptCode() {
		return deptCode;
	}
	public void setDeptCode(String deptCode) {
		this.deptCode = deptCode;
	}
	public Integer getRetentionId() {
		return retentionId;
	}
	public void setRetentionId(Integer retentionId) {
		this.retentionId = retentionId;
	}
	public Long getBeginSerial() {
		return beginSerial;
	}
	public void setBeginSerial(Long beginSerial) {
		this.beginSerial = beginSerial;
	}
	public Long getEndSerial() {
		return endSerial;
	}
	public void setEndSerial(Long endSerial) {
		this.endSerial = endSerial;
	}

}
